package com.leyou.cart.service;

import com.leyou.auth.pojo.UserInfo;
import com.leyou.cart.interceptor.UserInterceptor;
import com.leyou.cart.pojo.Cart;
import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exception.LyException;
import com.leyou.common.util.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundGeoOperations;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class CartService {
    @Autowired
    private StringRedisTemplate redisTemplate;

    private static final String KEY_PREIX="cart:uid:";

    /**
     * 购物车新增
     * @param cart
     */
    public void addCart(Cart cart) {
        //获取用户
        UserInfo userInfo = UserInterceptor.getUserInfo();


        //判断当前商品是否在购物车中存在，
        String key = KEY_PREIX + userInfo.getId();

        String hashKey = cart.getSkuId().toString();

        //从redis中获得当前key下的购物车
        BoundHashOperations<String, Object, Object> operation = redisTemplate.boundHashOps(key);

        //获得新增的购物车数量
        Integer num =cart.getNum();
        //判断当前购物车商品是否存在
        if (operation.hasKey(hashKey)) {
            //存在，修改数据,通过hashKey 获得对应的
            String json  = operation.get(hashKey).toString();
            //把json的cart转换为对象
            cart= JsonUtils.toBean(json, Cart.class);
            //修改数量
            cart.setNum(cart.getNum() + num);
        }

        //写会redis
        operation.put(hashKey,JsonUtils.toString(cart));
    }

    /**
     * 查询购物车
     * @return
     */
    public List<Cart> queryCartList() {
        //获取用户
        UserInfo userInfo = UserInterceptor.getUserInfo();

        //判断当前商品是否在购物车中存在，
        String key = KEY_PREIX + userInfo.getId();

        if (!redisTemplate.hasKey(key)) {
            throw  new LyException(ExceptionEnum.CART_NOT_FOUND);
        }
        //从redis中获得当前key下的购物车
        BoundHashOperations<String, Object, Object> operation = redisTemplate.boundHashOps(key);

        //把redis中的购物车转换为java的集合对象
        List<Cart> carts = operation.values().stream().map(o -> JsonUtils.toBean(o.toString(), Cart.class)).collect(Collectors.toList());


        return carts;

    }

    /**
     * 修改购物车
     * @param skuId
     * @param num
     */
    public void updateCartNum(Long skuId, Integer num) {
        //获取用户
        UserInfo userInfo = UserInterceptor.getUserInfo();

        //判断当前商品是否在购物车中存在，
        String key = KEY_PREIX + userInfo.getId();

        String haskKey = skuId.toString();

        BoundHashOperations<String, Object, Object> operations = redisTemplate.boundHashOps(key);
        if (!operations.hasKey(haskKey)) {
            throw  new LyException(ExceptionEnum.CART_NOT_FOUND);
        }
        //查询
        Cart cart = JsonUtils.toBean(operations.get(haskKey).toString(), Cart.class);

        cart.setNum(num);


        //写回redis
        operations.put(haskKey,JsonUtils.toString(cart));

    }

    /**
     * 删除购物车中的一件商品
     * @param skuId
     */
    public void deleteCart(Long skuId) {

        //获取用户
        UserInfo userInfo = UserInterceptor.getUserInfo();

        //判断当前商品是否在购物车中存在，
        String key = KEY_PREIX + userInfo.getId();

        String haskKey = skuId.toString();

        redisTemplate.opsForHash().delete(key,haskKey);
    }
}
